package com.imut.lagain.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.HumorMessage;
import com.imut.lagain.service.IHumorMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 幽默文案控制器
 */

@RestController
@RequestMapping("/api/humor-messages")
@RequiredArgsConstructor
public class HumorMessageController {
    private static final Logger log = LoggerFactory.getLogger(HumorMessageController.class);

    private final IHumorMessageService humorMessageService;

    /**
     * 创建幽默文案
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createHumorMessage(@RequestBody HumorMessage humorMessage) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            humorMessage.setCreateTime(LocalDateTime.now());
            humorMessage.setUpdateTime(LocalDateTime.now());
            
            boolean success = humorMessageService.save(humorMessage);
            if (success) {
                response.put("success", true);
                response.put("message", "幽默文案创建成功");
                response.put("data", humorMessage);
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "幽默文案创建失败");
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            log.error("创建幽默文案失败", e);
            response.put("success", false);
            response.put("message", "创建幽默文案失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 更新幽默文案
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateHumorMessage(@PathVariable Long id, @RequestBody HumorMessage humorMessage) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            HumorMessage existingMessage = humorMessageService.getById(id);
            if (existingMessage == null) {
                response.put("success", false);
                response.put("message", "幽默文案不存在");
                return ResponseEntity.notFound().build();
            }
            
            humorMessage.setId(id);
            humorMessage.setUpdateTime(LocalDateTime.now());
            
            boolean success = humorMessageService.updateById(humorMessage);
            if (success) {
                response.put("success", true);
                response.put("message", "幽默文案更新成功");
                response.put("data", humorMessage);
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "幽默文案更新失败");
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            log.error("更新幽默文案失败", e);
            response.put("success", false);
            response.put("message", "更新幽默文案失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 删除幽默文案
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteHumorMessage(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            HumorMessage existingMessage = humorMessageService.getById(id);
            if (existingMessage == null) {
                response.put("success", false);
                response.put("message", "幽默文案不存在");
                return ResponseEntity.notFound().build();
            }
            
            boolean success = humorMessageService.removeById(id);
            if (success) {
                response.put("success", true);
                response.put("message", "幽默文案删除成功");
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "幽默文案删除失败");
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            log.error("删除幽默文案失败", e);
            response.put("success", false);
            response.put("message", "删除幽默文案失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 获取幽默文案详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getHumorMessage(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            HumorMessage humorMessage = humorMessageService.getById(id);
            if (humorMessage != null) {
                response.put("success", true);
                response.put("message", "获取幽默文案成功");
                response.put("data", humorMessage);
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "幽默文案不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            log.error("获取幽默文案失败", e);
            response.put("success", false);
            response.put("message", "获取幽默文案失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 获取幽默文案列表
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getHumorMessageList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String moodType,
            @RequestParam(required = false) String thiefType) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            Page<HumorMessage> pageParam = new Page<>(page, size);
            IPage<HumorMessage> pageResult = humorMessageService.getHumorMessageList(pageParam, moodType, thiefType);
            
            response.put("success", true);
            response.put("message", "获取幽默文案列表成功");
            response.put("data", pageResult.getRecords());
            response.put("total", pageResult.getTotal());
            response.put("pages", pageResult.getPages());
            response.put("current", pageResult.getCurrent());
            response.put("size", pageResult.getSize());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取幽默文案列表失败", e);
            response.put("success", false);
            response.put("message", "获取幽默文案列表失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 获取随机幽默文案
     */
    @GetMapping("/random")
    public ResponseEntity<Map<String, Object>> getRandomHumorMessage(
            @RequestParam(required = false) String moodType,
            @RequestParam(required = false) String thiefType) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            HumorMessage humorMessage = humorMessageService.getRandomHumorMessage(moodType, thiefType);
            if (humorMessage != null) {
                response.put("success", true);
                response.put("message", "获取随机幽默文案成功");
                response.put("data", humorMessage);
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "暂无符合条件的幽默文案");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            log.error("获取随机幽默文案失败", e);
            response.put("success", false);
            response.put("message", "获取随机幽默文案失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
}